package com.autonavi.yunda.yunji.core.engine.scriptresolver;

/**
 * @author cizhong.wcs
 * @date 2022/6/20 11:02
 */
public class GroovyScriptWriter extends GeneratorWriter<GroovyScriptWriter> {

    protected GroovyScriptWriter() {
        super();
    }


    public GroovyScriptWriter printlnStaticImport(String string) {
        return super.println("import static %s", string);
    }

    public GroovyScriptWriter printlnImport(String string) {
        return super.println("import %s", string);
    }

    /**
     * 处理字符串中含有 \n的情况 转换为\\n 在脚本中不换行
     */
    public GroovyScriptWriter printString(String string, Object... args) {
        if (newline && indentTabs > 0) {
            for (int i = 0; i < indentTabs; i++) {
                sb.append(tabString);
            }
            newline = false;
            indentTabs = 0;
        }
        if (args.length > 0) {
            string = String.format(string, args);
        }
        string = string.replaceAll("\n", "\\\\n");
        sb.append(string);
        return this;
    }

    public GroovyScriptWriter printlnString(String string, Object... args) {
        printString(string, args);
        println();
        return this;
    }
}
